<?php
General::getTpl()->assign('extraTitle',"Admins");

if(!Admin::isLoggedIn()) {
	General::ouputMessage('Fehler','Sie haben nicht gen&uuml;gend Rechte um diese Seite zu betrachten');
}else{
	
	/**
	 * NEW ADMIN
	 */
	if(isset($_GET['do']) && $_GET['do'] == "new") {
		require_once 'kernel/form.php';
		
		// Form-Data
		$formField = array();
		$formField[0]['fieldName'] = "Name";
		$formField[0]['fieldType'] = Form::$TYPE_TEXT;
		$formField[0]['fieldDefaultValue'] = "";
		$formField[0]['fieldCheck'] = true;
		$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$formField[1]['fieldName'] = "Passwort";
		$formField[1]['fieldType'] = Form::$TYPE_PASSWORD;
		$formField[1]['fieldDefaultValue'] = "";
		$formField[1]['fieldCheck'] = true;
		$formField[1]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
		
		$formField[2]['fieldName'] = "Email";
		$formField[2]['fieldType'] = Form::$TYPE_TEXT;
		$formField[2]['fieldDefaultValue'] = "";
		$formField[2]['fieldCheck'] = true;
		$formField[2]['fieldCheckExtra'] = Form::$CHECK_TEXT_EMAIL;
		
		$form = new Form();
		$form->initByData("Admin anlegen",true,"Anlegen!",$formField);
		
		// Done?
		if($form->completeCheck()) {
			
			
			
			Admin::createAdmin($_POST['Name'],$_POST['Passwort'],$_POST['Email']);
			General::ouputMessage('Erfolgreich','Der Admin wurde erfolgreich angelegt.','form');
			
		}else{
			$form->outputFormular('form','formular.html');
		}
	}
	
	
	/**
	 * EDIT?
	 */
	if(isset($_GET['adminId']) && is_numeric($_GET['adminId']) && isset($_GET['do']) && $_GET['do'] == "edit") {
		if($_SESSION['ld_uid'] != $_GET['adminId'] && General::getInstance()->getGlobalVar('isFounder') == false) {
			General::ouputMessage('Fehler','Sie k&ouml;nnen nur sich selber bearbeiten!','form');
		}else{
			// GET ADMIN-DATA
			$query = DB::getInstance()->query("
				SELECT
					adminName,
					adminEmail
				FROM
					".Tables::$adminTbl."
				WHERE
					id = '".$_GET['adminId']."'");
			$data = DB::getInstance()->fetchArray($query);
			
			
			
			require_once 'kernel/form.php';
			
			// Form-Data
			$formField = array();
			$formField[0]['fieldName'] = "Name";
			$formField[0]['fieldType'] = Form::$TYPE_TEXT;
			$formField[0]['fieldDefaultValue'] = $data['adminName'];
			$formField[0]['fieldCheck'] = true;
			$formField[0]['fieldCheckExtra'] = Form::$CHECK_TEXT_NOTEMPTY;
			
			$formField[1]['fieldName'] = "Passwort";
			$formField[1]['fieldDescription'] = "Leer lassen wenn Passwort gleich bleiben soll";
			$formField[1]['fieldType'] = Form::$TYPE_PASSWORD;
			$formField[1]['fieldDefaultValue'] = "";
			$formField[1]['fieldCheck'] = false;
			
			$formField[2]['fieldName'] = "Email";
			$formField[2]['fieldType'] = Form::$TYPE_TEXT;
			$formField[2]['fieldDefaultValue'] = $data['adminEmail'];
			$formField[2]['fieldCheck'] = true;
			$formField[2]['fieldCheckExtra'] = Form::$CHECK_TEXT_EMAIL;
			
			$form = new Form();
			$form->initByData("Admin bearbeiten",true,"Bearbeiten!",$formField);
			
			// Done?
			if($form->completeCheck()) {
				
				Admin::editAdmin($_GET['adminId'],$_POST['Name'],$_POST['Passwort'],$_POST['Email']);
				General::ouputMessage('Erfolgreich','Der Admin wurde erfolgreich bearbeitet.','form');
				
			}else{
				$form->outputFormular('form','formular.html');
			}
		}
	}
	
	

	
	if(isset($_GET['adminId']) && is_numeric($_GET['adminId']) && isset($_GET['do']) && $_GET['do'] == "del") {
		if(General::getInstance()->getGlobalVar('isFounder') == false) {
			General::ouputMessage('Fehler','Nur der Gr&uuml;nder kann einen Admin l&ouml;schen!','form');
		}else{
			// Is Founder?
			$query = DB::getInstance()->query("
				SELECT
					adminFounder
				FROM
					".Tables::$adminTbl."
				WHERE
					id = '".$_GET['adminId']."'");
			$res = DB::getInstance()->fetchArray($query);
			
			if($res['adminFounder'] == 0) {
				General::ouputMessage('Fehler','Der Gr&uuml;nder kann nicht gel&ouml;scht werden.','form');
			}else{
			
				require_once './kernel/formCheck.php';
				
				$form = new FormCheck("Bist du sicher?","L&ouml;schen");
				// Done?
				if($form->completeCheck()) {
					if($form->isChecked()) {
						// Delete navigation
						Admin::deleteAdmin($_GET['adminId']);
					}
				}else{
					$form->outputFormular('form','formular.html');
				}
			}
		}
	}
	
	
	
	
	/**
	 * EDIT ADMIN RIGHTS
	 */
	if(isset($_GET['adminId']) && is_numeric($_GET['adminId']) && isset($_GET['do']) && $_GET['do'] == "editRights") {
		if(General::getInstance()->getGlobalVar('isFounder') == false) {
			General::ouputMessage('Fehler','Nur der Gr&uuml;nder kann die Rechte ver&auml;ndern!','form');
		}else{
			require_once 'kernel/form.php';
			
			// Form-Data
			$formField = array();
			$i = 0;
			// Get Rights
			$adminRights = Admin::getAdminRightsCompl($_GET['adminId']);
			foreach($adminRights as $data) {
				$formField[$i]['fieldName'] = $data['rightOutput'];
				$formField[$i]['formName'] = $data['rightName'];
				$formField[$i]['fieldType'] = Form::$TYPE_SELECT;
				$formField[$i]['fieldDefaultValue'] = ($data['value'] == 0) ? "Ja":"Nein";
				$formField[$i]['fieldCheck'] = false;
				$formField[$i]['fieldSelectOptions'] = "Ja::--::Nein";
				$formField[$i]['fieldSelectMultiple'] = false;
				$i++;
			}
			
			$form = new Form();
			$form->initByData("Adminrechte bearbeiten",true,"Bearbeiten!",$formField);
			
			// Done?
			if($form->completeCheck()) {
				
				// Save Rights
				$adminRights = Admin::getAdminRightsCompl($_GET['adminId']);
				foreach($adminRights as $data) {
					$newValue = ($_POST[$data['rightName']][0] == "Ja") ? 0:1;
					if($data['value'] != $newValue) {
						// Update
						// Is there already an entry?
						if($data['rightToAdminId'] == "") {
							DB::getInstance()->query("
								INSERT INTO
									".Tables::$rightToAdminTbl."
								SET
									adminId = '".$_GET['adminId']."',
									rightId = '".$data['rightId']."',
									value = '".$newValue."'");
						}else{
							
							DB::getInstance()->query("
								UPDATE
									".Tables::$rightToAdminTbl."
								SET
									value = '".$newValue."'
								WHERE
									adminId = '".$_GET['adminId']."' &&
									rightId = '".$data['rightId']."'");
						}
					}
				}
				
				General::ouputMessage('Erfolgreich','Die Admin-Rechte wurden erfolgreich bearbeitet.','form');
				
			}else{
				$form->outputFormular('form','formular.html');
			}
		}
	}
	
	
	
	$query = DB::getInstance()->query("
		SELECT
			id,
			adminName,
			adminEmail,
			adminFounder
		FROM
			".Tables::$adminTbl."
		ORDER BY
			id ASC");
	$admins = array();
	while($a = DB::getInstance()->fetchArray($query)) {
		$admins[] = $a;
	}
	General::getTpl()->assign('admins',$admins);
	
	General::getTpl()->assign('body',
		General::getTpl()->fetch('../admin/admins.html'));
}
?>